package cn.admin.config.common.protocol;

import com.caucho.hessian.io.AbstractSerializerFactory;
import com.caucho.hessian.io.Deserializer;
import com.caucho.hessian.io.HessianProtocolException;
import com.caucho.hessian.io.Serializer;

import java.lang.reflect.Field;

public class SwizzleCodec extends AbstractSerializerFactory {

    static Field f_bytes;
    static Field f_object;
    static Field f_serialization;

    private SwizzleEncoder encoder = new SwizzleEncoder();
    private SwizzleDecoder decoder = new SwizzleDecoder();

    static {
        try {
            f_bytes = Swizzle.class.getDeclaredField("bytes");
            f_bytes.setAccessible(true);
            f_object = Swizzle.class.getDeclaredField("object");
            f_object.setAccessible(true);
            f_serialization = Swizzle.class.getDeclaredField("serialization");
            f_serialization.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Serializer getSerializer(Class cl) throws HessianProtocolException {
        return cl == Swizzle.class ? this.encoder : null;
    }

    @Override
    public Deserializer getDeserializer(Class cl) throws HessianProtocolException {
        return Swizzle.class == cl ? this.decoder : null;
    }
}
